/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package au.com.gworks.jump.app.wiki.client;

import java.util.Date;
import java.util.Map;

import org.javaongems.client.Gem;
import org.javaongems.client.forms.Form;
import org.javaongems.client.forms.FormController;
import org.javaongems.client.stdctrls.Image;


import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class Alert {
	static public class Dialog extends Form {
		final public String ALERT_IMAGE = Gem.stone.getApplImageCtx() + "alert.png";
		public Image alertImage = new Image(ALERT_IMAGE);
		private DockPanel clientArea = new DockPanel();
		private ScrollPanel scroller = new ScrollPanel();
		private Controller controller;
		private CellFormatter cellFormatter;
		/* pkg */Button clearMessages = new Button("clear messages");
		/* pkg */FlexTable errorMessageView = new FlexTable();

		public Dialog() {
			initialiseLayout();
		}

		private void initialiseLayout() {
			setCaption("Error messages");
			setPixelSize(450, 200);
			add(clientArea);
			clientArea.add(alertImage, DockPanel.WEST);
			clientArea
					.setCellVerticalAlignment(alertImage, DockPanel.ALIGN_TOP);
			scroller.add(errorMessageView);
			clientArea.add(clearMessages, DockPanel.SOUTH);
			clientArea.add(scroller, DockPanel.CENTER);
			clientArea.setCellVerticalAlignment(clearMessages,
					DockPanel.ALIGN_BOTTOM);
			clientArea.setCellHorizontalAlignment(clearMessages,
					DockPanel.ALIGN_RIGHT);

			String imgPfx = Gem.stone.getApplImageCtx();
			com.google.gwt.user.client.ui.Image formIcon = new Image(imgPfx
					+ "alert-x16.png");
			setFormIcon(formIcon);

			errorMessageView.setStyleName("errorDialogMessages");
			cellFormatter = errorMessageView.getCellFormatter();
			setCloseFormButtonDisplayed(false);
			setMaxFormButtonDisplayed(false);
		}

		public void setController(Controller ctrlr) {
			controller = (Controller) ctrlr;
			super.setController(controller);
			setFormActionListener(controller);
		}

		public void alertMessage(String htmlMsg) {
			errorMessageView.insertRow(0);
			errorMessageView.insertCell(0, 0);
			cellFormatter.setStyleName(0, 0, "gwt-HTML");
			Date now = new Date();
			String time = now.toLocaleString();
			errorMessageView.setHTML(0, 0, time);
			errorMessageView.insertCell(0, 1);
			cellFormatter.setStyleName(0, 1, "gwt-HTML");
			errorMessageView.setHTML(0, 1, htmlMsg);

			show();
		}
	}

	static public class Controller extends FormController implements ClickListener {
		Dialog window;

		public Object getModel() {
			return null;
		}

		public void setUp(Map config) {
			super.setUp(config);
			window = new Dialog();
			window.setController(this);
			setForm(window);
			window.clearMessages.addClickListener(this);
		}

		public void reportStatus(String msg) {
			window.setStatus(msg);
		}

		public void alert(String msg) {
			window.alertMessage(msg);
		}

		public void onClick(Widget sender) {
			if (sender == window.clearMessages) {
				while (window.errorMessageView.getRowCount() > 0)
					window.errorMessageView.removeRow(0);
			}
		}
	}
}